package com.aiinterview.subjectservice.controller.user;


import cn.hutool.core.bean.BeanUtil;
import com.aiinterview.common.constant.JwtClaimsConstant;
import com.aiinterview.common.entity.Result;
import com.aiinterview.common.util.JwtUtil;
import com.aiinterview.model.subject.entity.LoginUser;
import com.aiinterview.model.subject.entity.User;
import com.aiinterview.model.subject.vo.UserVo;
import com.aiinterview.subjectservice.service.user.UserLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.aiinterview.common.properties.JwtProperties;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
@Slf4j
public class SysLoginController {

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private UserLoginService userLoginService;

    @PostMapping ("/login")
    private Result<UserVo> login(@RequestBody LoginUser loginUser){
        // 登录并返回用户相关信息
        User user = userLoginService.login(loginUser);
        if(user == null) {
            return Result.fail("账号或密码为空！");
        }
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        UserVo userVo = new UserVo();
        BeanUtil.copyProperties(user,userVo);
        userVo.setToken(token);
        return Result.ok(userVo);
    }
}
